#include "Package.h"
#include "LED.h"

module ReceiveC {
	uses interface Receive;
	uses interface LEDControl;
	uses interface CC2420Packet;
	uses interface Data;
	uses interface StateDataInterface;
}
implementation {
	bool toggle = TRUE;
	Package pack;

	void ToggleLed() {
		if(toggle == TRUE) 
			call LEDControl.On(LEDState_ModeReceive);
		else 
			call LEDControl.Off(LEDState_ModeReceive);

		toggle = ! toggle;
	}

	event message_t * Receive.receive(message_t * msg, void * payload,
			uint8_t len) {
		ToggleLed();

		pack = *((Package * ) payload);

		pack.rssi = (call CC2420Packet.getRssi(msg)) - 45; //The offset is about -45 due to the specification.  

		call StateDataInterface.DataReceived(&pack);

		ToggleLed();

		return msg;
	}
}